<?php

class Nav extends Component {
	var $params = array (
		'title',
		'URL',
		'template',
		'editFormTemplate',
		'style',
		'targetId',
		'targetClass'
	);
	public $children = false;

	public static $actions = array (
		'editFormSave'
	);
		
	function __construct ($id = false, $url = false, $params = array ()) {
		$this->id = $id;
		$this->URL = $url;
		
		$this->targetId = false;
		$this->targetClass = false;
		
		if ($this->id != false) {
			$this->load ();
		}
		else if ($this->URL != false) {
			$this->load ('URL');
		}
		else {
			foreach ($params as $key=>$value) {
				$this->{$key} = $value;
			}
		}
	}
	
	function createTable () {
		global $dbh;
  
  		$tableName = get_class ($this);
		
		$query = <<<QUERY
CREATE TABLE `{$tableName}` (
  `id` char(32) NOT NULL,
  `title` varchar(100) NOT NULL,
  `URL` varchar(100) NOT NULL,
  `style` varchar(50),
  `template` varchar(50),
  `editFormTemplate` varchar(50),
  `targetId` char(32),
  `targetClass` varchar(50),
  PRIMARY KEY  (`id`),
  KEY `title_index` (`title`),
  KEY `URL_index` (`URL`),
  KEY `comId_comClass_index` (`targetId`, `targetClass`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
QUERY;
		$dbh->exec ($query);
		
		return true;
	}
	
	function assignChild ($nav) {
		$this->saveIfNew ();
		NavHasNav::relate ($this, $nav);
	}
	
	function unassignChild ($nav) {
		NavHasNav::unrelate ($this, $nav);
	}
	
	function assignComponent ($target) {
		$this->targetId = $target->id;
		$this->targetClass = get_class ($target);		
	}
	
	function unassignComponent() {
		$this->targetId = false;
		$this->targetClass = false;
	}
	
	function getComponent () {
		if ($this->targetId && $this->targetClass) {
			return eval ("return new {$this->targetClass} ('{$this->targetId}');");
		}
		else {
			return false;
		}
	}
	
	function navigate ( $urlArray = array () ) {
		global $dbh;
		if (count ($urlArray) > 0) {
			if ($this->isRegisteredAction ($urlArray [0])) {
				return $this->{array_shift ($urlArray)} ($urlArray);
			}
			
			$query = new ObjLib\Query (array (
				'tables' => array ('Nav', 'Nav'),
				'relations' => 'Has',
				'where' => "level0.id = {$dbh->quote ($this->id)} and level1.URL = {$dbh->quote ($urlArray[0])}"
			));
			$child = $query->select ();
			
			if (count($child) > 0) {
				array_shift ($urlArray);
				return $child[0]->navigate ($urlArray);
			}
			else {
				$com = $this->getComponent ();
				if ($com->isRegisteredAction ($urlArray[0])) {
					return $com->{array_shift ($urlArray)} ($urlArray);
				}
				else {
					return false;
				}
			}
		}
		else {
			if ($com = $this->getComponent()) {
				return $com->render ();
			}
			else {
				return false;
			}
		}
	}
	
	function render () {
		if (!$this->children) {
			$this->getChildren ();
		}
				
		parent::render ();
	}
	
	function getChildren () {
		global $dbh;
		
		$query = new ObjLib\Query (array(
			'tables' => array ('Nav', 'Nav'),
			'relations' => 'Has',
			'where' => "level0.id = {$dbh->quote ($this->id)}"
		));
		
		$this->children = $query->select ();
	}
	
	function fullURL () {
		global $rootUrl;
		return $rootUrl . '/index.php/' . $this->URL;
	}
	
	function isRegisteredAction ($action) {
		if ($action == 'render') {
			return true;
		}
		else {
			return (array_search ($action, static::$actions) !== false);
		}
	}
};

?>